import neurokit2 as nk
import matplotlib.pyplot as plt

# 1. 加载示例数据
ecg_signal = nk.ecg_simulate(duration=10, sampling_rate=1000)

# 2. 预处理并定位特征点
cleaned = nk.ecg_clean(ecg_signal, sampling_rate=1000)
signals, info = nk.ecg_peaks(cleaned, sampling_rate=1000, method="neurokit")

# 3. 可视化R峰检测
nk.events_plot(info["ECG_R_Peaks"], cleaned)

# 4. 提取更全面的特征（如P波、QRS复合波、T波）
ecg_events = nk.ecg_findpeaks(cleaned, sampling_rate=1000)
waves = nk.ecg_delineate(cleaned, ecg_events, sampling_rate=1000, method="peak")

# 5. 绘制详细的心电图波形
nk.ecg_plot(waves, sampling_rate=1000)
plt.show()

# 6. 计算心率变异性（HRV）指标
hrv_features = nk.hrv(ecg_events, sampling_rate=1000, show=True)
print(hrv_features)